import { httpDelete, httpGet, httpPost } from '@/http'
import { services } from '@/config'
import { getStore } from '@/util/store'

/**
 * 获取租户列表
 */
export const getTenantList = () => httpGet(`${services.adminService}tenant/list`)

/**
 * 获取菜单
 */
export const getMenu = () => httpGet(`${services.adminService}menu`)

/**
 * 用户登录
 */
export const login = ({ username, password, grantType, randomStr, validCode }) => {
  const basicAuth = `Basic ${window.btoa('pig:pig')}`
  return httpPost(
    '/auth/oauth/token',
    { username, password },
    true,
    { randomStr, code: validCode, grant_type: grantType },
    {
      header: {
        isToken: false,
        'TENANT-ID': getStore('tenantId'),
        Authorization: basicAuth
      }
    }
  )
}

export const loginByMobile = ({ code, grantType, mobile }) => {
  const basicAuth = `Basic ${window.btoa('pig:pig')}`
  return httpPost(
    '/auth/oauth/token',
    null,
    true,
    { code, mobile, grant_type: grantType },
    {
      header: {
        isToken: false,
        'TENANT-ID': '1',
        Authorization: basicAuth
      }
    }
  )
}

/**
 * 登出
 */
export const logout = () => httpDelete(`${services.authService}token/logout`)

/**
 * 删除 redis 中的用户信息
 */
export const deleteRedisUsers = () => httpDelete(`${services.adminService}user/deleteRedisUser`)
